package cj.web.admin.controllers;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.var.Model;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.DateUtils;
import cj.utils.ExportExcel;
import cj.web.admin.dao.GameDAO;
import cj.web.admin.dao.PermitDAO;
import cj.web.admin.domain.Game;
import cj.web.admin.domain.Permit;
import cj.web.constants.GameTypeEnum;
import cj.web.constants.MemuEnum;
import cj.web.constants.PermitStatusEnum;
import cj.web.login.controllers.BaseController;

@Path("permitExport")
public class PermitExportController extends BaseController {

	private static Logger logger = Logger
			.getLogger(PermitExportController.class.getName());

	private final static int excelCount = 50000;

	@Autowired
	private GameDAO gameDao;
	@Autowired
	private PermitDAO permitDao;

	@Get("show")
	public String show(Model model) {
		model.add("MemuEnum", MemuEnum.csshdc);
		List<Map> objs = gameDao.asGameEnum(Integer.parseInt(GameTypeEnum.FF.getValue()));
		model.add("gameObjs", objs);
		return "/views/game/permitExportList.vm";
	}

	@Get("output")
	public void output(@Param("gameId") int gameId, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Game game = gameDao.queryById(gameId);

		String title = URLEncoder.encode(game.getName(), "UTF-8"); // 进行转码，使其支持中文文件名
		response.setContentType("application/vnd.ms-excel"); // 生成提示信息
		response.setHeader("content-disposition", "attachment;filename="
				+ title + DateUtils.getNowTime(DateUtils.DATE_FULL_STR1)
				+ ".xls");
		OutputStream fOut = response.getOutputStream();

		// 参赛证总数
		int count = permitDao.querySize(gameId);
		double sheetSize = Double.parseDouble(String.valueOf(count))
				/ excelCount;
		int sheetCount = (int) Math.ceil(sheetSize);

		ExportExcel<Permit> ex = new ExportExcel<Permit>();
		// 声明一个工作薄
		HSSFWorkbook workbook = new HSSFWorkbook();

		for (int i = 0; i < sheetCount; i++) {

			List<Map> dataset = null;

			// 信息Export
			String[] headers = { "区域", "学校", "学校地址", "参赛证号", "密码", "回执码", "状态" };
			String[] fields = { "areaName", "schoolName","address", "code", "pwdShow",
					"returnCode", "statusName" };

			dataset = permitDao.queryByGameId(gameId, i * excelCount,
					excelCount);
			for (Map rep : dataset) {
				String statusName = PermitStatusEnum.getName(Integer
						.valueOf(rep.get("status").toString()));
				rep.put("statusName", statusName);
			}

			ex.exportExcelMore(workbook,game.getName() + "-" + i * excelCount, headers, fields,
					dataset, DateUtils.DATE_FULL_STR);
		}
		
		try {
			//关闭工作薄
			workbook.write(fOut);
		} catch (IOException e) {
			e.printStackTrace();
		}

		fOut.flush();
		fOut.close();
	}

	
}